//
//  NewsCollectionViewController.m
//  YouWen
//
//  Created by liqiang－pc on 15/3/11.
//  Copyright (c) 2015年 刘永和. All rights reserved.
//

#import "NewsCollectionViewController.h"

@interface NewsCollectionViewController ()

@end

@implementation NewsCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height - 64)] ;
    
    [self.view addSubview:self.myTableView];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
//    self.myTableView.separatorColor = [UIColor B];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    
    [self.myTableView setEditing:editing animated:animated];
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  UITableViewCellEditingStyleDelete;
    
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//设置section个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    [[HeadLineDataHandle shareHeadLineDataHandle] openDB];
    return [[[HeadLineDataHandle shareHeadLineDataHandle] selectHead] count];
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1.删数据库表内容
    [[HeadLineDataHandle shareHeadLineDataHandle] openDB];
    //要删除的model对象
    self.headLine = [[[HeadLineDataHandle   shareHeadLineDataHandle] selectHead] objectAtIndex:indexPath.row];
    
    [[HeadLineDataHandle shareHeadLineDataHandle]deleteHeadLine:self.headLine];
    
    //2.删除cell内容
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
    
}
//设置删除按钮标题
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}



//创建cell 并设置cell上内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //一个重用池里可能有多个不同的cell,为了区别不同的cell,要给cell设置重用标示符
    //1.定义cell重用标示符
    static NSString *cellIdentifier = @"cell";
    //2.从重用池里取出cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    //3.判断取出来的cell是否为空,如果是空,则创建新的
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    [[HeadLineDataHandle shareHeadLineDataHandle] openDB];
    cell.textLabel.text = [[[[HeadLineDataHandle shareHeadLineDataHandle] selectHead] objectAtIndex:indexPath.row] title];
    
    
    
    return cell;
}

//点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HeadDetailsViewController *headDetailsVC = [[HeadDetailsViewController alloc] init];
    
    headDetailsVC.headLine = [[[HeadLineDataHandle shareHeadLineDataHandle] selectHead] objectAtIndex:indexPath.row];
    NSLog(@"44444444 %@",headDetailsVC.headLine.contents);
    headDetailsVC.title = @"新闻收藏";
    [self.navigationController pushViewController:headDetailsVC animated:YES];
    
    
    
}

    
    
    
    
       



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
